package com.yimi.yinhepda.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.entity.bean.CheckEntity;

import java.util.List;

/**
 * @author 000432
 * @date 2017/4/11 0011
 * <p>
 * 查看运单信息的adapter
 */
public class CheckBillAdapter extends BaseAdapter {

    private Context mContext;
    private List<CheckEntity> mEntityList;
    /**
     * 滞留原因list
     */
    private List<String> mRetentionList;

    public CheckBillAdapter(Context context, List<CheckEntity> entityList, List<String> retentionList) {
        this.mContext = context;
        this.mEntityList = entityList;
        this.mRetentionList = retentionList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_check_waybill_detail_item, null);
            viewHolder.childWayBillNo =  convertView.findViewById(R.id.compare_listitem_waybillno);
            viewHolder.status =  convertView.findViewById(R.id.compare_listitem_scanestate);
            viewHolder.retention =  convertView.findViewById(R.id.tv_retention);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        CheckEntity entity = mEntityList.get(position);

        //运单号
        viewHolder.childWayBillNo.setText(entity.getChildWayBillNo());
        int status = entity.getStatus();
        if (status == 0) {
            viewHolder.status.setText("未扫描");
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.red));
        } else if (status == 1) {
            viewHolder.status.setText("已扫描");
            convertView.setBackgroundColor(mContext.getResources().getColor(R.color.green));
        }
        if (mRetentionList != null && entity.getRetention() != null) {
            try {
                viewHolder.retention.setText(mRetentionList.get(Integer.parseInt(entity.getRetention()) - 1));
            } catch (NumberFormatException e) {
                viewHolder.retention.setText(entity.getRetention());
            }
        }

        return convertView;
    }

    public static class ViewHolder {
        /**
         * 运单号
         */
        TextView childWayBillNo;
        /**
         * 状态
         */
        TextView status;
        /**
         * 滞留原因
         */
        TextView retention;
    }

    @Override
    public int getCount() {
        return mEntityList != null ? mEntityList.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return mEntityList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
}
